<template>
  <div>
    <!--查询条件start --111s-->
    <el-form :inline="true" :model="query" class="query-form" size="small">
      <!--            <el-form-item>-->
      <!--                <el-button type="primary" icon="el-icon-edit" @click.native="handleForm()">添加</el-button>-->
      <!--            </el-form-item>-->

      <el-form-item>
        <el-date-picker
          v-model="createTime"
          type="datetimerange"
          :picker-options="pickerOptions"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="left"
          format="yyyy-MM-dd HH:mm:ss"
          value-format="yyyy-MM-dd HH:mm:ss"
          :default-time="['00:00:00', '23:59:59']"
          @change="dateChange"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="评分：">
        <el-select
          v-model="query.evaluatetype"
          placeholder="请选择"
          @change="shelfstatus"
          clearable
        >
          <el-option label="好评" value="1"></el-option>
          <el-option label="中评" value="2"></el-option>
          <el-option label="差评" value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item class="query-form-item" size="small">
        <el-input
          v-model="query.usernick"
          placeholder="请输入用户名"
          prefix-icon="el-icon-search"
          @keyup.enter.native="onSubmit"
          clearable
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-refresh"
          @click="onRest"
          size="small"
        ></el-button>
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="onSubmit"
          size="small"
          >查询</el-button
        >
      </el-form-item>
    </el-form>

    <!--列表start --222s-->
    <el-table
      ref="multipleTable"
      v-loading="loading"
      :data="list"
      :header-cell-style="{ background: '#F4F4F5', color: '#606266' }"
      @selection-change="handleSelectionChange"
      border
    >
      <el-table-column type="selection" width="50" fixed />
      <el-table-column label="ID" prop="commentid" width="50" align="center" />

      <el-table-column
        label="评论人"
        prop="usernick"
        width="200"
        align="center"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.usernick != null">{{
            scope.row.usernick
          }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>

      <el-table-column
        label="规格参数"
        prop="commodityspecification"
        width="200"
        align="center"
      >
        <template slot-scope="scope">
                          
          <span
            > {{
              scope.row.commodityspecification
                .toString()
                .replace(/\"/g, "")
                .replace(/\{|}/g, "")
            }}</span
          >
                      
        </template>
      </el-table-column>
      <el-table-column
        label="评分"
        prop="evaluatetype"
        width="200"
        align="center"
      >
        <template slot-scope="scope">
          <!--   <div>
                        <span>{{scope.row.evaluatetype}}</span>
                    </div>-->
          <div v-if="scope.row.evaluatetype === 1">
            <span>好评</span>
          </div>
          <div v-if="scope.row.evaluatetype === 2">
            <span>中评</span>
          </div>
          <div v-if="scope.row.evaluatetype === 3">
            <span>差评</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="评价内容"
        prop="evaluatecontent"
        width="200"
        align="center"
      />
      <el-table-column
        label="添加时间"
        with="200"
        :show-overflow-tooltip="true"
        align="center"
      >
        <template slot-scope="scope">
          <i class="el-icon-time" v-if="scope.row.creationtime != null"></i>
          <span v-if="scope.row.creationtime != null">{{
            scope.row.creationtime | formatDateStr("yyyy-MM-dd hh:mm:ss")
          }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click.native="detailForm(scope.$index, scope.row)"
            >查看
          </el-button>
          <el-button
            type="danger"
            size="mini"
            @click.native="handleDel(scope.row.commentid)"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--列表end --222e-->
    <div class="left-container">
      <el-button type="danger" size="small" @click="delGroup()"
        >删除已选</el-button
      >
      <transition name="el-fade-in-linear">
        <el-button size="small" @click="toggleSelection()" v-show="cancelToggle"
          >取消选择</el-button
        >
      </transition>
    </div>
    <!--分页模块start-->
    <el-pagination
      class="pagination-container"
      background
      :page-size="query.pageSize"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      layout="total,sizes,prev, pager, next, jumper"
      :current-page="query.currentPage"
      :page-sizes="[5, 10, 20, 30, 50]"
      :total="totalNumber"
    ></el-pagination>
    <!--分页模块end-->
    <!--查看弹框-->
    <el-dialog
      title="查看"
      :visible.sync="detailVisible"
      :before-close="hideDetailForm"
      width="1000px"
      top="5vh"
      v-dialogDrag
    >
      <el-form :model="formData" label-width="140px">
        <el-form-item
          style="border-bottom: 1px solid #ebeef5"
          label="评论人昵称："
        >
          <span v-if="formData.usernick != null">{{ formData.usernick }}</span>
          <span v-else>无</span>
        </el-form-item>
        <el-form-item
          style="border-bottom: 1px solid #ebeef5"
          label="评论时间："
        >
          <span>{{ formData.creationtime | formatDateStr("yyyy-MM-dd") }}</span>
        </el-form-item>

        <el-form-item
          style="border-bottom: 1px solid #ebeef5"
          label="评价内容："
        >
          <span>{{ formData.evaluatecontent }}</span>
        </el-form-item>
        <el-form-item
          label="评论图："
          style="width: 30%; display: inline-block"
        >
          <template>
            <div
              v-for="(item, index) in formData.piclist"
              :key="index"
              style="width: 300px; display: inline-block"
            >
              <el-popover
                v-if="item.commentpic"
                placement="right"
                trigger="hover"
              >
                <img
                  :src="item.commentpic"
                  style="width: 400px; border-radius: 3px"
                />
                <img
                  slot="reference"
                  :src="item.commentpic"
                  style="
                    max-height: 200px;
                    max-width: 200px;
                    border-radius: 5px;
                  "
                />
              </el-popover>
              <span v-else>-</span>
            </div>
          </template>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import {
  getComment,
  getCommentParameters,
  delComment,
  updateBusCommodityComment,
  insertComment,
} from "../../api/buscommodity/buscommodity.js";
/*用作表单的数据初始化*/

export default {
  data() {
    return {
      pickerOptions: {
        //快速选择日期
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近六个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 180);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      query: {
        //查询条件
        commodityid: "",
        usernick: "",
        starttime: "",
        endtime: "",
        evaluatetype: "", //1:好 2:中 3:差
        currentPage: 1,
        pageSize: 5,
      },
      formData: {}, //新增或修改的对象
      createTime: [], //选择日期时需要的model
      list: [], //列表集合
      totalNumber: 0, //分页总页数
      loading: true, //列表加载
      cancelToggle: false,
      detailVisible: false, //查看
      multipleSelection: [], //多选集合
    };
  },
  methods: {
    shelfstatus() {
      this.query.currentPage = 1;
      this.getList();
    },
    //显示 查看弹框
    detailForm(index, row) {
      if (row !== null) {
        this.detailVisible = true;
        this.formData = Object.assign({}, row);
        console.info(row);
      }
    },

    /*获取列表事件*/
    getList() {
      this.loading = true;
      getComment(this.query)
        .then((response) => {
          if (response.code != 0) {
            this.$message.error(response.message);
            return false;
          }
          this.list = response.data.list || [];
          // this.list = response.data.list || [];

          this.totalNumber = response.data.totalNumber || 0;
          console.info(this.list);
          setTimeout(() => {
            this.loading = false;
          }, 0.3 * 1000);
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
        });
    },

    /*日期选择事件*/
    dateChange(val) {
      if (val == null) {
        this.query.starttime = "";
        this.query.endtime = "";
      } else {
        this.query.starttime = val[0];
        this.query.endtime = val[1];
        this.query.currentPage = 1;
      }
      this.getList();
      console.log(this.query);
    },
    /*列表查询条件事件*/
    onSubmit() {
      this.query.currentPage = 1;
      this.getList();
    },

    /*刷新*/
    onRest() {
      this.createTime = [];
      this.query.usernick = "";
      this.query.evaluatetype = "";
      this.query.starttime = "";
      this.query.endtime = "";
      this.query.currentPage = 1;
      this.getList();
    },

    /*点击页数*/
    handleCurrentChange(val) {
      this.query.currentPage = val;
      this.getList();
    },

    /*选择每页条数【10,20,30,40,50】*/
    handleSizeChange(val) {
      this.query.pageSize = val;
      this.getList();
    },

    /*取消全选事件*/
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
      this.dialogVisible = false;
    },

    /*列表复选框选择事件*/
    handleSelectionChange(val) {
      this.multipleSelection = val;
      if (val == "") {
        this.cancelToggle = false;
        return;
      }
      this.cancelToggle = true;
    },

    /*全选删除事件*/
    delGroup() {
      console.log(this.multipleSelection);
      var ids = this.multipleSelection.map((item) => item.commentid).join(); //获取所有选中行的id组成的字符串，以逗号分隔
      if (ids == "") {
        this.$message({
          message: "请先选择",
          type: "warning",
        });
        return;
      }
      this.handleDel(ids);
    },

    /*选中事件*/
    handleDel(ids) {
      if (ids) {
        this.$confirm("确认要删除吗?", "提示", {
          type: "warning",
        })
          .then(() => {
            this.loading = true;
            var params = new URLSearchParams();
            params.append("ids", ids);
            delComment(params)
              .then((response) => {
                this.loading = false;
                if (response.code) {
                  this.$message.error(response.message);
                  return false;
                }
                this.$message.success("删除成功");
                // 刷新数据
                //this.list.splice(index, 1);
                this.getList();
              })
              .catch(() => {
                this.loading = false;
              });
          })
          .catch(() => {
            this.$message.warning("已取消");
          });
      }
    },

    // 隐藏详情弹框
    hideDetailForm() {
      // 更改值
      this.detailVisible = !this.detailVisible;
      return true;
    },
  },

  created() {
    let flag = this.$route.query.cid;
    if (flag !== undefined) {
      this.query.commodityid = flag;
      this.getList();
    }
  },
};
</script>
